using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using PoemGame.PoemService;
using PoemGame.Repositories.EF.SqlServer;
using PoemGame.SignalR.Backend;
using SignalRApplication;
using SignalRApplication.Contracts;
using System.Reflection;
using WebApp;
using WebApp.Data;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers()
    .AddApplicationPart(Assembly.Load(new AssemblyName("PoemGame.SignalR.Backend")));
;
builder.Services.AddSignalR();
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.AddPoemGameEF(builder.Configuration);

builder.Services
        .AddPoemLineService()
        //.AddSingleton<IPoemService, ServiceSqlite>()
        .AddGameDomainService(builder.Configuration)
        .AddScoped<ISignalRAppService, SignalRAppService>();
WebApplication app;
try
{
    app = builder.Build();
}
catch (Exception ex)
{

    throw ex;
}


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapHub<SingleGameHub>("/singlegamehub");

app.MapRazorPages();

app.Run();
